package databasetest;

import java.awt.EventQueue;

import javax.naming.spi.DirStateFactory.Result;
import javax.swing.JInternalFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

import java.awt.BorderLayout;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.Rectangle;

public class Select extends JInternalFrame {
	private JPanel panel;
	private JTextField textField;
	private JButton btnNewButton;
	private JScrollPane scrollPane;
	private JTable table;
	DbTest dt;
	Vector<String> v = new Vector<String>();
	DefaultTableModel model;
	Connection con;
	private PreparedStatement pstmtTotalScroll;
	private PreparedStatement pstmtTotal;
	private ResultSet rsScroll;
	private ResultSet rs;
	private ResultSetMetaData rsmd;
	Update update;

	/**
	 * Launch the application.
	 */

	/**
	 * Create the frame.
	 */
	public Select(Connection c, DbTest d) {
		super("Select", true, true, true, false);
		getContentPane().setBounds(new Rectangle(100, 100, 900, 800));
		setBounds(new Rectangle(100, 100, 900, 900));

		setBounds(100, 100, 576, 312);
		getContentPane().setLayout(null);
		getContentPane().add(getPanel());
		getContentPane().add(getScrollPane());
		getContentPane().add(getBtnNewButton());
		con = c;
		this.dt = d;

	}

	private JPanel getPanel() {
		if (panel == null) {
			panel = new JPanel();
			panel.setBounds(0, 0, 434, 35);
			panel.setLayout(new BorderLayout(0, 0));
			panel.add(getTextField());
		}
		return panel;
	}

	private JTextField getTextField() {
		if (textField == null) {
			textField = new JTextField();
			textField.setColumns(10);
		}
		return textField;
	}

	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("쿼리실행");
			btnNewButton.setBounds(442, 0, 81, 35);
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					// search();
					setList();
			
				}
			});
		}
		return btnNewButton;
	}

	public void setList() {
		
		String s = textField.getText();
		try {
			PreparedStatement pstmt = con.prepareStatement(s);
			ResultSet rs = pstmt.executeQuery();
			ResultSetMetaData rsm = rs.getMetaData();
			Vector v = new Vector<>();
			 v.add("No");
			for (int i = 1; i <= rsm.getColumnCount(); i++) {
				v.add(rsm.getColumnName(i));
			}
			model = new DefaultTableModel(v, 0);
			table.setModel(model);

			int cnt = 1;
			while (rs.next()) {
				Vector d = new Vector<>();
				d.add(cnt);
				for (int i = 1; i <= rsm.getColumnCount(); i++) {
//					System.out.println(rsm.getColumnCount());
					d.add(rs.getString(i));
				}
				
				model.addRow(d);
				cnt++;
			}
		
		} catch (Exception ex) {
			System.out.println("예외발생");
		}
	}

	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setBounds(0, 45, 523, 210);
			scrollPane.setViewportView(getTable());
		}
		return scrollPane;
	}

	private JTable getTable() {
		if (table == null) {
			table = new JTable(model);
			table.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent ev) {
					// mouse();
					if (dt.update == null) {
						dt.openUpdate();
					}
					else{
						dt.openUpdate();
					}
					int row = table.getSelectedRow();
					String s = (String) table.getValueAt(row, 1);
					String c = (String) table.getValueAt(row, 2);
					String e = (String) table.getValueAt(row, 3);

					dt.update.setData(s, c, e);
					dt.update.setVisible(true);
					// dt.update.toFront();

				}
			});
		}
		return table;
	}
}
